home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / lists / mint / l_0799 / 508 < prev    next >
Encoding:
Internet Message Format  |  1994-08-27  |  1.7 KB

  1. From: Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
  2. Date: Tue, 21 Sep 93 11:13:23 +0200
  3. Message-Id: <9309210913.AA15703@issan.informatik.uni-dortmund.de>
  4. To: mint@atari.archive.umich.edu
  5. Subject: truncate and ftruncate for the MiNTlib
  6.  
  7. Here is an implementation of truncate/ftruncate for the MiNTlib. There
  8. is a special case for truncate, if the filesystem does not recognize
  9. FTRUNCATE and the length is zero, Fcreate is used to truncate the
  10. file. This only works for tosfs if the file isn't already open. The
  11. rest is quite straight forward.
  12.  
  13. Andreas.
  14.  
  15. P.S.: Sorry for the bogus mail i sent out yesterday. Just one wrong
  16. word... :-(
  17.  
  18. ------------------------- cut here -------------------------
  19. #include <compiler.h>
  20. #include <limits.h>
  21. #include <errno.h>
  22. #include <mintbind.h>
  23. #include <ioctl.h>
  24. #ifdef __TURBOC__
  25. #include <sys\types.h>
  26. #else
  27. #include <sys/types.h>
  28. #endif
  29. #include "lib.h"
  30.  
  31. extern int __mint;
  32.  
  33. int
  34. truncate (_filename, length)
  35.      const char *_filename;
  36.      off_t length;
  37. {
  38.   int fh, res;
  39.   char filename[PATH_MAX];
  40.  
  41.   (void) _unx2dos (_filename, filename);
  42.   fh = Fopen (filename, 2);
  43.   if (fh < 0)
  44.     {
  45.       errno = -fh;
  46.       return -1;
  47.     }
  48.   res = -EINVAL;
  49.   if (__mint > 90)
  50.     res = Fcntl (fh, (long) &length, FTRUNCATE);
  51.   Fclose (fh);
  52.   if (res == -EINVAL && length == 0)
  53.     {
  54.       res = Fcreate (filename, 0);
  55.       if (res >= 0)
  56.     Fclose (res);
  57.     }
  58.   if (res < 0)
  59.     {
  60.       errno = -res;
  61.       return -1;
  62.     }
  63.   return 0;
  64. }
  65.  
  66. int
  67. ftruncate (fd, length)
  68.      int fd;
  69.      off_t length;
  70. {
  71.   int res;
  72.  
  73.   if (__mint > 90)
  74.     res = Fcntl (fd, (long) &length, FTRUNCATE);
  75.   else
  76.     res = -EINVAL;
  77.  
  78.   if (res < 0)
  79.     {
  80.       errno = -res;
  81.       return -1;
  82.     }
  83.   return 0;
  84. }
  85. ------------------------- cut here -------------------------
  86.